import http from '../../utils/http.js'
const apiConfig = require('../../utils/apiConfig.js')
Page({
  data: {
    selectedManufacturer: null,
    selectedGoods: null,
    form: {
      now_date: '',
      manufacturer_id: '',
      goods_id: '',
      cost_price: 0,
      num: 0,
      total_price: 0
    },
    manufacturerList: [],
    goodsList: [],
    currentManufacturerPage: 1,
    currentGoodsPage: 1,
    per_page: 10,
    loading: false,
    isLoadingMore: false
  },

  // 加载厂家列表
  loadManufacturerList() {
    if (this.data.isLoadingMore) return
    this.setData({ isLoadingMore: true })
    http.request({
      url: apiConfig.getManufacturerList,
      data: {
        page: this.data.currentManufacturerPage,
        per_page: this.data.per_page
      }
    }).then(res => {
      if (res.status == 200) {
        this.setData({
          manufacturerList: this.data.manufacturerList.concat(res.data.data),
          currentManufacturerPage: this.data.currentManufacturerPage + 1,
          isLoadingMore: false
        }) 
      }
    })
  },

  // 加载产品列表
  loadGoodsList() {
    if (this.data.isLoadingMore) return
    this.setData({ isLoadingMore: true })
    http.request({
      url: apiConfig.getGoodsList,
      data: {
        page: this.data.currentGoodsPage,
        per_page: this.data.per_page,
        manufacturer_id: this.data.form.manufacturer_id
      }
    }).then(res => {
      if (res.status == 200) {
        this.setData({
          goodsList: this.data.goodsList.concat(res.data.data),
          currentGoodsPage: this.data.currentGoodsPage + 1,
          isLoadingMore: false
        })
      }
    })
  },

  // 日期选择处理
  bindDateChange(e) {
    this.setData({
      'form.now_date': e.detail.value
    })
  },

  // 厂家选择处理
  handleManufacturerChange(e) {
    const manufacturer = this.data.manufacturerList[e.detail.value];
    this.setData({
      'form.manufacturer_id': manufacturer.id,
      selectedManufacturer: manufacturer,
      'form.goods_id': '',
      'form.cost_price': 0,
      goodsList: [],
      currentGoodsPage: 1
    })
    this.loadGoodsList()
  },

  // 产品选择处理
  handleGoodsChange(e) {
    const index = e.detail.value
    const goods = this.data.goodsList[index]
    this.setData({
      'form.goods_id': goods.id,
      'form.cost_price': goods.cost_price,
      selectedGoods: goods
    })
    this.calculateTotal()
  },

  // 数量输入处理
  bindNumInput(e) {
    this.setData({
      'form.num': e.detail.value
    }, () => {
      this.calculateTotal()
    })
  },

  // 计算总金额
  calculateTotal() {
    const { cost_price, num } = this.data.form
    this.setData({
      'form.total_price': (cost_price * num).toFixed(2)
    })
  },

  // 表单提交
  formSubmit(e) {
    const { now_date, manufacturer_id, goods_id, num } = this.data.form
    if (!now_date || !manufacturer_id || !goods_id || !num) {
      wx.showToast({ title: '请填写完整信息', icon: 'none' })
      return
    }
    this.setData({ loading: true })
    http.request({
      url: apiConfig.addRestock,
      method: 'POST',
      data: this.data.form,
    }).then(res => {
      if (res.status == 200) {
        wx.showToast({ title: '提交成功', icon: 'success' }) 
        this.setData({
          form: {
            now_date: '',
            manufacturer_id: '',
            goods_id: '',
            cost_price: 0,
            num: 0,
            total_price: 0
          },
          manufacturerList: [],
          goodsList: [],
          currentManufacturerPage: 1,
          currentGoodsPage: 1,
          loading: false
        })
      }else {
        wx.showToast({ title: '提交失败', icon: 'none' })
      }
    })
  },

  // 触底加载更多
  onReachBottom() {
    if (this.data.form.manufacturer_id) {
      this.loadGoodsList()
    } else {
      this.loadManufacturerList()
    }
  },

  onLoad() {
    this.loadManufacturerList()
  }
})